﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace BlogModel.DAL
{
    public partial class PostDB
    {
        public static int Save()
        {
            try
            {
                return DB.Context.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                Exception raise = dbEx;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        string message = string.Format("{0}:{1}",
                            validationErrors.Entry.Entity.ToString(),
                            validationError.ErrorMessage);
                        // raise a new exception nesting
                        // the current instance as InnerException
                        raise = new InvalidOperationException(message, raise);
                    }
                }
                throw raise;
            }
        }

        public static List<Post> GetPostsOnBlogId(int blogId) //Fixad! Testad och OK!
        {
            return DB.Context.PostSet
                .OrderBy(u=>u.CreationDate)
                .Where(u => u.BlogId==blogId && !u.IsDeleted).ToList();
        }

        public static Post GetPostByPostId(int postId)
        {
            return DB.Context.PostSet
                     .FirstOrDefault(p => p.PostId == postId && !p.IsDeleted);
        }




           //var custSuppJoin = suppliers
           //         .Join(customers, s => s.Country, c => c.Country, (s, c) => new
           //         {
           //             Country = s.Country,
           //             SupplierName = s.SupplierName,
           //             CustomerName = c.CompanyName
           //         });

           //     foreach (var item in custSuppJoin)
           //     {
           //         Console.WriteLine("Country = {0}, Supplier = {1}, Customer = {2}", item.Country, item.SupplierName, item.CustomerName);
           //     }

        public static int InsertPost(Post post)      //Fixad! Testad och OK!
        {
            //try
            {
                DB.Context.PostSet.Add(post);
                int affectedRows = DB.Context.SaveChanges();
                return affectedRows;

            }
            //catch (Exception e)
            //{
            //    throw new MyAppException(e);
            //}
        }

        public static List<Post> GetPosts(string nameIsLike) //Fixad! Testad och OK!
        {
            //try
            {
                var query =
                    from p in DB.Context.PostSet
                    orderby p.CreationDate
                    select p;
                List<Post> result = query.ToList();
                return result;
            }
            //catch (Exception e)
            //{
            //    throw new MyAppException(e);
            //}
        }

        public static List<Post> GetAllPosts()
        {
            return DB.Context.PostSet
                .OrderBy(p=>p.CreationDate)
                .Where(p => !p.IsDeleted).ToList();
        }

        public static int UpdatePost(Post post) 
        {
            DB.Context.PostSet.AddOrUpdate(p=>p.PostId, post);
            return DB.Context.SaveChanges();
        }

        public static int DeletePost(int id)
        {
            var postToDelete = DB.Context.PostSet
                .FirstOrDefault(p => p.PostId == id);
            
            if(postToDelete != null)
                postToDelete.IsDeleted = true;

            return DB.Context.SaveChanges();
        }


        public static List<Comment> PrintCommentsOnPostId(int postId)  //Klar! Testad och OK!
        {
            return DB.Context.CommentSet
                .Where(c => c.PostId == postId && !c.IsDeleted).ToList();
        }
    }
}
